<?php
class reviews {
	private $episode_id;
	private $reviewPerPage = 5;
	private $recentNumber = 5;
	
	function __construct($episode_id = 0){
		$this->episode_id = $episode_id;
	}
	
	public function addReview($text){
		global $db, $user;
		
		if(!$user->isLoggedIn())
			return false;
		$text = trim(strip_tags($text));
		$text = $db->real_escape_string($text);		
		
		$sql = "INSERT INTO `" . REVIEW_TABLE . "` (`user_id`, `episode_id`, `review`) VALUES ('{$user->data['id']}', '{$this->episode_id}', '{$text}')";
		return $db->query($sql);
	}
	
	public function getReviews($p = 1){
		global $db;
		$all = $this->numberOfReviews();
		$start = max(0, $all - ($p * $this->reviewPerPage));
		$sql = "SELECT u.user_email as `revOwner`, u.user_settings, r.review, r.date_added as `revTime`, a.rating FROM `" . REVIEW_TABLE . "` r LEFT JOIN `" . USERS_TABLE . "` u on u.`id` = r.`user_id` LEFT JOIN `" . RANKING_TABLE . "` a on a.`user_id` = r.`user_id` and a.`episode_id` = r.`episode_id`  WHERE r.`episode_id` = '{$this->episode_id}'  ORDER BY r.`date_added` ASC LIMIT {$start}, {$this->reviewPerPage}";

		if($db->query($sql))
		{
			$result = $db->fetch_assoc();
			
			if(is_array($result))
			foreach($result as &$item){
				$item['review'] = stripslashes($item['review']);
				if(!isset($item['revOwner']))
					$item['revOwner'] = 'Anonymous';
				$item['user_settings'] = (isset($item['user_settings'])) ? json_decode($item['user_settings'], true) : array();

				if(isset($item['user_settings']['fb']['proxied_email']))
					unset($item['user_settings']['fb']['proxied_email']);
				if(isset($item['user_settings']['fb']['uid']))
					unset($item['user_settings']['fb']['uid']);
			}
			return $result;
		}else
		{
			return false;
		}
		
	}
	public function pageCount(){
		$all = $this->numberOfReviews();
		return (int)ceil($all/$this->reviewPerPage);
	}
	
	private function numberOfReviews(){
		global $db;
		$sql = "SELECT `date_added` FROM `" . REVIEW_TABLE . "` WHERE `episode_id` = '{$this->episode_id}'";
		$db->query($sql);
		return $db->affected_rows();	
	}
	
	public function recentReviews(){
		global $db;

		$sql = "SELECT u.user_email as `revOwner`, u.user_settings, r.review, r.date_added as `revTime`, s.seriesname, e.episodenumber, e.seasonnumber FROM `" . REVIEW_TABLE . "` r LEFT JOIN `" . USERS_TABLE . "` u on u.`id` = r.`user_id` LEFT JOIN `" . EPISODES_TABLE . "` e on r.episode_id = e.id LEFT JOIN `" . SERIES_TABLE . "` s on e.seriesid = s.id ORDER BY r.`date_added` DESC LIMIT {$this->recentNumber}";

		if($db->query($sql) && $db->affected_rows())
		{
			$result = $db->fetch_assoc();

			foreach($result as &$item){
				$item['review'] = stripslashes($item['review']);
				$item['niceTime'] = nicetime($item['revTime']);
				
				if(!isset($item['revOwner']))
					$item['revOwner'] = 'Anonymous';
				$item['user_settings'] = (isset($item['user_settings'])) ? json_decode($item['user_settings'], true) : array();
				
				if(isset($item['user_settings']['fb']) and is_array($item['user_settings']['fb']))
				{
					$item['revOwner'] = (!empty($item['user_settings']['fb']['username'])) ? $item['user_settings']['fb']['username'] : $item['user_settings']['fb']['last_name'] . " ". $item['user_settings']['fb']['first_name'];
				}
								
			}
			return $result;
		}else
		{
			return false;
		}
	}
}
?>